package com.redbox.RedboxManagment.dao;


import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.redbox.RedboxManagment.ejb.User;

@Repository(value = "userDao")
public class UserImp extends HibernateDaoSupport implements IUser {
	
	@SuppressWarnings("unchecked")
	@Transactional
	public User findById(int pId) {
		
		String queryString= "from User a where a.idUser=?";
		List<User> result =  getHibernateTemplate().find(queryString,pId);
		
		if (result.size() > 0) {
	            return result.get(0);
	        } else {
	            return null;
	        }   
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public User findByName(String name) {
		String queryString= "from User a where a.userName like ?";
		List<User> result = getHibernateTemplate().find(queryString,name);
		
		if (result.size() > 0) {
            return result.get(0);
        } else {
            return null;
        }   
		
	}

	@Transactional
	public User persistOnMerge(User pUser) {
		User result = (User) getHibernateTemplate().save(pUser);
		return result;
	}

	@Override
	@Transactional
	public List<User> findAll() {
		
		String queryString= "from User";
		List<User> result =  getHibernateTemplate().find(queryString);
		return result;
	}

}
